<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true">
        <el-form-item>
          <el-input v-model="keyword" placeholder="请输入关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="keywordQuery">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <!--
       :data="employees" - 绑定数据
       highlight-current-row - 当前行高亮
       v-loading="listLoading" - 加载框=盲等框
       @selection-change="selsChange" - 选中时间
       sortable - 是否排序
 -->
    <el-table :data="employees" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="username" label="员工名字" width="100">
      </el-table-column>
      <el-table-column prop="phone" label="员工号码" width="110">
      </el-table-column>
      <el-table-column prop="age" label="年龄" width="60">
      </el-table-column>
      <el-table-column prop="state" label="状态" width="80" sortable>
        <template scope="scope">
          <span v-if="scope.row.state==1" style="color: green">启用</span>
          <span v-else style="color: red">禁用</span>
        </template>
      </el-table-column>
      <el-table-column prop="department.name" label="所属部门" width="80">
      </el-table-column>
      <el-table-column prop="shop.name" label="店铺" min-width="60" sortable>
      </el-table-column>
      <el-table-column label="操作">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
                     :current-page="currentPage" :page-size="pageSize" :total="totals" style="float:right;">
      </el-pagination>
    </el-col>

    <!--添加或修改的对话框-->
    <el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="100px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="员工姓名" prop="username">
          <el-input v-model="saveForm.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="员工电话" prop="phone">
          <el-input v-model="saveForm.phone" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="员工邮箱" prop="email">
          <el-input v-model="saveForm.email" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="员工年龄" prop="age">
          <el-input v-model="saveForm.age" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="员工状态">
          <el-radio-group v-model="saveForm.state">
            <el-radio class="radio" :label="1">启用</el-radio>
            <el-radio class="radio" :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="所属部门" prop="department_id">
          <!--<el-input v-model="saveForm.department_id" auto-complete="off"></el-input>-->
          <el-cascader v-model="saveForm.department_id"
                       :options="deptTree"
                       :props="{ checkStrictly: true,label:'name',value:'id' }"
                       clearable></el-cascader>
        </el-form-item>
        <el-form-item label="店铺" prop="shop_id">
          <el-input v-model="saveForm.shop_id" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>

export default {
  data() {
    return {
      //1.数据列表相关的模型数据
      employees: [],
      listLoading: false,

      //2.分页查询数据
      totals: 0,
      currentPage: 1,
      pageSize: 5,

      //3.高级查询
      keyword: '',

      //批量删除
      sels: [],//列表选中列

      //添加和修改
      title: '',
      saveFormVisible: false,  //添加和修改的对话框是否显示
      saveLoading: false,     //保存的盲等框,在提交按钮上
      saveFormRules: {        //表单的验证规则
        username: [
          {required: true, message: '请输入员工名称', trigger: 'blur'}
        ]
      },
      //对话框数据
      saveForm: {
        id: null,
        username: '',
        phone: '',
        email: '',
        age: null,
        state: 1,
        department_id: null,
        logininfo_id: null,
        shop_id: null
      },
      deptTree: []
    }
  },
  methods: {

    handleCurrentChange(val) {
      this.currentPage = val;
      this.getEmployees();
    },
    keywordQuery() {
      this.currentPage = 1,
          this.getEmployees()
    },
    //获取员工列表
    getEmployees() {
      //参数
      let para = {
        "currentPage": this.currentPage,
        "pageSize": this.pageSize,
        "keyword": this.keyword
      };
      //显示盲等框
      this.listLoading = true;
      //NProgress.start();
      this.$http.post("/employee", para).then(res => {
        //关闭盲等框
        this.listLoading = false;
        this.employees = res.data.data;
        this.totals = res.data.totals;
      }).catch(res => {
        this.$message.error("系统繁忙,请稍后再试!!!")
      })
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', { //确认框
        type: 'warning'
      }).then(() => {//点击确定的时候
        this.listLoading = true;
        this.$http.delete("/employee/" + row.id).then(res => {
          this.listLoading = false;//关闭忙等框
          if (res.data.success) { //成功
            this.$message.success("删除成功");
            let totalPage = Math.ceil(this.totals / this.pageSize);
            if (this.currentPage == totalPage && (this.totals - 1) % this.pageSize == 0 && this.currentPage != 1) {
              this.currentPage = this.currentPage - 1;
            }
            //对当前页的处理
            this.getEmployees();
          } else {
            this.$message.error("网络繁忙，请稍后重试!!!【500】")
          }
        }).catch(res => {
          this.$message.error("网络繁忙，请稍后重试!!!【400,404】")
        })
      }).catch(() => { //点击取消的时候
      });
    },
    //点击修改按钮显示对话框
    handleEdit: function (index, row) {
      this.saveForm = Object.assign({}, row);
      this.title = "修改",
          this.getDeptTree();
      this.saveFormVisible = true;
    },
    //点击添加按钮显示对话框
    handleAdd: function () {
      this.saveForm = {
        id: null,
        username: '',
        phone: '',
        email: '',
        age: null,
        state: 1,
        department_id: null,
        logininfo_id: null,
        shop_id: null
      };
      this.title = "添加",
          this.getDeptTree();
      this.saveFormVisible = true;
    },
    //提交添加或修改的数据
    saveSubmit: function () {
      this.$refs.saveForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.saveLoading = true;
            let para = Object.assign({}, this.saveForm);
            let arr = para.department_id;
            if (arr && arr.length == 0) {
              para.department_id = null;
            }
            if (arr && arr.length > 0) { //[1,13]
              para.department_id = arr[arr.length - 1];
            }
            this.$http.put("/employee", para).then(res => {
              this.saveLoading = false;
              this.saveFormVisible = false;
              if (res.data.success) {
                this.$message.success("操作成功");
                this.getEmployees();
              } else {
                this.$message.error("网络繁忙，请稍后重试!!!【500】")
              }
            }).catch(res => {
              this.$message.error("网络繁忙，请稍后重试!!!【400,404】")
            })
          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.patch("/employee", ids).then(res => {
          this.listLoading = false;
          if (res.data.success) {
            this.$message.success("删除成功");
            this.currentPage = 1;
            this.getEmployees();
          } else {
            this.$message.error("网络繁忙,请稍后重试!!!")
          }
        }).catch(res => {
          this.$message.error("网络繁忙,请稍后重试!!!")
        })
      }).catch(() => {
      });
    },
    getDeptTree() {
      this.$http.get("/department/deptTree").then(res => {
        this.deptTree = res.data;
      })
    }
  },
  mounted() {
    this.getEmployees();
  }
}

</script>

<style scoped>

</style>